package com.mengchengnet.chaperonage.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.ui.calendar.EventDecorator_Holiday;
import com.mengchengnet.chaperonage.ui.calendar.EventDecorator_Workday;
import com.mengchengnet.chaperonage.ui.calendar.HighlightWeekendsDecorator;
import com.mengchengnet.chaperonage.ui.calendar.LunarDecorator;
import com.mengchengnet.chaperonage.ui.calendar.TodayDecorator;
import com.mengchengnet.chaperonage.util.DateUtils;
import com.mengchengnet.chaperonage.util.HolidaysManager;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.OnMonthChangedListener;
import com.prolificinteractive.materialcalendarview.OnRangeSelectedListener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2017/8/21.
 */

public class CalanderActivity extends BaseActivity implements OnMonthChangedListener,OnRangeSelectedListener

{
    private MaterialCalendarView widget;
    private CalendarDay today;
    public LunarDecorator mLunarDecorator;
    public String year;
    public String month;
    private HolidaysManager holidaysManager;
    private static final String TAG = "CalanderActivity";
    private String mode;//日历的选择方式
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calander_popwindow);
        toolbarState=1;
        initToolbar("日期选择");
        button= (Button) findViewById(R.id.pop_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        widget= (MaterialCalendarView) findViewById(R.id.calendarView_calendar);
        holidaysManager = new HolidaysManager();
        today = CalendarDay.today();

        year = DateUtils.date2String(today.getDate(), "yyyy");
        month = DateUtils.date2String(today.getDate(), "MM");
        mLunarDecorator = new LunarDecorator(year,month);

        widget.setCurrentDate(today);
        widget.setSelectedDate(today);
        //在头和尾显示其他月的日期
        widget.setShowOtherDates(MaterialCalendarView.SHOW_DEFAULTS);
        //当点击头和尾处的其他月的日期时，跳转到其他月
        widget.setAllowClickDaysOutsideCurrentMonth(true);
        widget.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                int n=date.getMonth()+1;
                String s=date.getYear()+"-"+n+"-"+date.getDay();
                int n1=today.getMonth()+1;
                String s1=today.getYear()+"-"+n1+"-"+today.getDay();
                Log.e("选中的日期：", s);
                Log.e("当前日期：", s1);
                Log.e("日期：", date.getDate().toString());
                Log.e("selected：", selected+"");
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
                Date dateSelect=DateUtils.string2Date(s,"yyyy-MM-dd");
                Date dateNow=DateUtils.string2Date(s1,"yyyy-MM-dd");

                long r=DateUtils.dayDiff(dateNow,dateSelect);
                Log.e("日期比较的结果", "onDateSelected: "+r );
                if (r>=0){
                    Intent intent =new Intent();
                    intent.putExtra("result", s);
                    CalanderActivity.this.setResult(RESULT_OK,intent);
                    //CalanderActivity.this.finish();
                }

            }
        });
        widget.setOnRangeSelectedListener(this);
        widget.setOnMonthChangedListener(this);
        mode=getIntent().getStringExtra("mode");
        if (mode.equals("0")){
            widget.setSelectionMode(3);
        }else{
            widget.setSelectionMode(1);
        }


        widget.addDecorators(
                new TodayDecorator(),//当前日期的标志
                mLunarDecorator,//显示农历
//                new MySelectorDecorator(this),//点击日期的标志
                new HighlightWeekendsDecorator(),//周末日期的标志
                new EventDecorator_Holiday(holidaysManager.getHolidays()),
                new EventDecorator_Workday(holidaysManager.getHolidays()),
                new EnableOneToTenDecorator()
        );
//        holidaysManager = new HolidaysManager();
//        today=CalendarDay.today();
//        year = DateUtils.date2String(today.getDate(), "yyyy");
//        month = DateUtils.date2String(today.getDate(), "MM");
//        mLunar = new Lunar(today.getDate());
//        mLunarDecorator = new LunarDecorator(year,month);
//        widget.setCurrentDate(today);
//        widget.setSelectedDate(today);
//        //在头和尾显示其他月的日期
//        widget.setShowOtherDates(MaterialCalendarView.SHOW_DEFAULTS);
//        //当点击头和尾处的其他月的日期时，跳转到其他月
//        widget.setAllowClickDaysOutsideCurrentMonth(true);
//        //calendarView.state().edit().setMinimumDate(today).commit();
//
//        widget.setSelectionMode(1);
//        //calendarView.setHeaderTextAppearance();//重设顶部年月指示器的文本外观
//        widget.addDecorators(
//                //new TodayDecorator(),//当前日期的标志
//                mLunarDecorator,//显示农历
////                new MySelectorDecorator(this),//点击日期的标志
//                new HighlightWeekendsDecorator(),//周末日期的标志
//                //new EventDecorator_Holiday(holidaysManager.getHolidays()),
//                new EventDecorator_Workday(holidaysManager.getHolidays()),
//                new EnableOneToTenDecorator()
//        );
//        widget.setOnDateChangedListener(new OnDateSelectedListener() {
//            @Override
//            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
//                int n=date.getMonth()+1;
//                String s=date.getYear()+"-"+n+"-"+date.getDay();
//                int n1=today.getMonth()+1;
//                String s1=today.getYear()+"-"+n1+"-"+today.getDay();
//                Log.e("选中的日期：", s);
//                Log.e("当前日期：", s1);
//                Log.e("日期：", date.getDate().toString());
//                Log.e("selected：", selected+"");
//                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
//                Date dateSelect=DateUtils.string2Date(s,"yyyy-MM-dd");
//                Date dateNow=DateUtils.string2Date(s1,"yyyy-MM-dd");
//
//                Log.e(TAG, "onDateSelected: "+mLunar.toString());
//                Log.e(TAG, "onDateSelected: "+mLunar.animalsYear());
//                Log.e(TAG, "onDateSelected: "+mLunar.cyclical());
//                long r=DateUtils.dayDiff(dateNow,dateSelect);
//                Log.e("日期比较的结果", "onDateSelected: "+r );
//                if (r>=0){
//                    Intent intent =new Intent();
//                    intent.putExtra("result", s);
//                    CalanderActivity.this.setResult(RESULT_OK,intent);
//                   // CalanderActivity.this.finish();
//                }
//
//
//            }
//        });
//        widget.setOnRangeSelectedListener(new OnRangeSelectedListener() {
//            @Override
//            public void onRangeSelected(@NonNull MaterialCalendarView widget, @NonNull List<CalendarDay> dates) {
//                Log.e("复选的日期集合", "onRangeSelected: "+dates.toString() );
//            }
//        });
//        widget.setOnMonthChangedListener(new OnMonthChangedListener() {
//            @Override
//            public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
//                mLunarDecorator.setYear(DateUtils.date2String(date.getDate(), "yyyy"));
//                mLunarDecorator.setMonth(DateUtils.date2String(date.getDate(), "MM"));
//                widget.invalidateDecorators();
//                Log.e(TAG, "onMonthChanged: "+date.getMonth());
//                Log.e(TAG, "onMonthChanged2: "+DateUtils.getCurrMonth());
//                if (date.getMonth()>DateUtils.getCurrMonth()-1){
//                    widget.addDecorators(
//                            //new TodayDecorator(),//当前日期的标志
//                            mLunarDecorator,//显示农历
////                new MySelectorDecorator(this),//点击日期的标志
//                            new HighlightWeekendsDecorator(),//周末日期的标志
//                            //new EventDecorator_Holiday(holidaysManager.getHolidays()),
//                            new EventDecorator_Workday(holidaysManager.getHolidays())
//                            //new EnableOneToTenDecorator()
//                    );
//                }else if(date.getMonth()==DateUtils.getCurrMonth()-1){
//                    widget.addDecorators(
//                            //new TodayDecorator(),//当前日期的标志
//                            mLunarDecorator,//显示农历
////                new MySelectorDecorator(this),//点击日期的标志
//                            new HighlightWeekendsDecorator(),//周末日期的标志
//                            //new EventDecorator_Holiday(holidaysManager.getHolidays()),
//                            new EventDecorator_Workday(holidaysManager.getHolidays()),
//                            new EnableOneToTenDecorator()
//                    );
//                }else{
//                    widget.addDecorators(
//                            //new TodayDecorator(),//当前日期的标志
//                            mLunarDecorator,//显示农历
////                new MySelectorDecorator(this),//点击日期的标志
//                            new HighlightWeekendsDecorator(),//周末日期的标志
//                            //new EventDecorator_Holiday(holidaysManager.getHolidays()),
//                            new EventDecorator_Workday(holidaysManager.getHolidays()),
//                            new TotalDecorator()
//                    );
//                }
//
//                Log.e("月份：", date.toString() );
//            }
//        });


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
                mLunarDecorator.setYear(DateUtils.date2String(date.getDate(), "yyyy"));
                mLunarDecorator.setMonth(DateUtils.date2String(date.getDate(), "MM"));
                widget.invalidateDecorators();
                Log.e(TAG, "onMonthChanged: "+date.getMonth());
                Log.e(TAG, "onMonthChanged2: "+DateUtils.getCurrMonth());
                if (date.getMonth()>DateUtils.getCurrMonth()-1){
                    widget.addDecorators(
                            //new TodayDecorator(),//当前日期的标志
                            mLunarDecorator,//显示农历
//                new MySelectorDecorator(this),//点击日期的标志
                            new HighlightWeekendsDecorator(),//周末日期的标志
                            //new EventDecorator_Holiday(holidaysManager.getHolidays()),
                            new EventDecorator_Workday(holidaysManager.getHolidays())
                            //new EnableOneToTenDecorator()
                    );
                }else if(date.getMonth()==DateUtils.getCurrMonth()-1){
                    widget.addDecorators(
                            //new TodayDecorator(),//当前日期的标志
                            mLunarDecorator,//显示农历
//                new MySelectorDecorator(this),//点击日期的标志
                            new HighlightWeekendsDecorator(),//周末日期的标志//new EventDecorator_Holiday(holidaysManager.getHolidays()),
                            new EventDecorator_Workday(holidaysManager.getHolidays()),
                            new EnableOneToTenDecorator()
                    );
                }else{
                    widget.addDecorators(
                            //new TodayDecorator(),//当前日期的标志
                            mLunarDecorator,//显示农历
//                new MySelectorDecorator(this),//点击日期的标志
                            new HighlightWeekendsDecorator(),//周末日期的标志
                            //new EventDecorator_Holiday(holidaysManager.getHolidays()),
                            new EventDecorator_Workday(holidaysManager.getHolidays()),
                            new TotalDecorator()
                    );
                }

    }

    @Override
    public void onRangeSelected(@NonNull MaterialCalendarView widget, @NonNull List<CalendarDay> dates) {
        Log.e("复选的日期集合", "onRangeSelected: "+dates.toString() );
        String first=dates.get(0).getYear()+"-"+dates.get(0).getMonth()+"-"+dates.get(0).getDay();
        String last=dates.get(dates.size()-1).getYear()+"-"+dates.get(dates.size()-1).getMonth()+"-"+dates.get(dates.size()-1).getDay();
        Log.e(TAG, "range1: "+first );
        Log.e(TAG, "range2: "+last );
        Intent intent =new Intent();
        intent.putExtra("result", first+","+last);
        CalanderActivity.this.setResult(RESULT_OK,intent);
        CalanderActivity.this.finish();

    }


    /**
     * 日期禁用设置
     */
    private  class EnableOneToTenDecorator implements DayViewDecorator {

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return day.getDay() <today.getDay() ;
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setDaysDisabled(true);
        }
    }

    private  class TotalDecorator implements DayViewDecorator {

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return day.getDay() <32 ;
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setDaysDisabled(true);
        }
    }



    }



